/**
 * 
 */
package com.hjx.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjx.dao.ArticleMapper;
import com.hjx.dao.CategoryMapper;
import com.hjx.model.Article;
import com.hjx.model.ArticleCriteria;
import com.hjx.model.Category;
import com.hjx.model.CategoryCriteria;
import com.hjx.model.CategoryCriteria.Criteria;
import com.hjx.model.Page;
import com.hjx.service.ICategoryService;

/**
 * @author huangjixin
 * 
 */
@Service(value = "categoryService")
@RemotingDestination(value = "categoryService",channels= {"my-amf","my-secure-amf"})
@Transactional
public class CategoryServiceImpl extends AbstractGenericService<Category, Integer> implements
		ICategoryService {

	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private ArticleMapper articleMapper;
	
	@Override
	public List<Category> selectAll() {
		List<Category> list = categoryMapper.selectByExample(new CategoryCriteria());
		return list;
	}

	@Override
	public Page selectByPage(Page page) {
		CategoryCriteria categoryCriteria = new CategoryCriteria();
		categoryCriteria.setStart(page.getStart());
		categoryCriteria.setLimit(page.getLimit());
		int count = categoryMapper.countByExample(new CategoryCriteria());
		List<Category> list = categoryMapper.selectByExample(categoryCriteria);
		page.setCount(count);
		page.setList(list);
		
		return page;
	}
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		//-- 先删除子表数据
		ArticleCriteria articleCriteria = new ArticleCriteria();
		articleCriteria.createCriteria().andCategoryIdEqualTo(id);
		List<Article> list0 = articleMapper.selectByExample(articleCriteria);
		for (Article article : list0) {
			articleMapper.deleteByPrimaryKey(article.getId());
		}
				
		List<Category>list = getByParentId(id);
		for (Category category : list) {
			deleteByPrimaryKey(category.getId());
		}
				
		int i = categoryMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public int insert(Category record) {
		super.insert(record);
		int i = categoryMapper.insert(record);
		return i;
	}

	@Override
	public int insertSelective(Category record) {
		super.insertSelective(record);
		int i = categoryMapper.insertSelective(record);
		return i;
	}

	@Override
	public Category selectByPrimaryKey(Integer id) {
		Category category = categoryMapper.selectByPrimaryKey(id);
		return category;
	}

	@Override
	public int updateByPrimaryKeySelective(Category record) {
		int i = categoryMapper.updateByPrimaryKeySelective(record);
		return i;
	}

	@Override
	public int updateByPrimaryKey(Category record) {
		int i = categoryMapper.updateByPrimaryKey(record);
		return i;
	}

	@Override
	public List<Category> getByParentId(Integer id) {
		CategoryCriteria categoryCriteria = new CategoryCriteria();
		categoryCriteria.createCriteria().andParentidEqualTo(id);
		List<Category> list = categoryMapper.selectByExample(categoryCriteria);
		return list;
	}

	@Override
	public List<JSONObject> getCategory() {
		CategoryCriteria categoryCriteria = new CategoryCriteria();
		Criteria criteria = categoryCriteria.createCriteria();
		criteria.andParentidIsNull();
//		criteria.andIdIn(ids);

		List<Category> list = categoryMapper.selectByExample(categoryCriteria);
		List<JSONObject> children = new ArrayList<JSONObject>();
		for (Category object : list) {
			JSONObject jsonObject = searialTcatgory(object);
			children.add(jsonObject);
		}
		return children;
	}


	private JSONObject searialTcatgory(Category category) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", category.getId());
		jsonObject.put("parentid", category.getParentid());
		jsonObject.put("name", category.getName());
//		jsonObject.put("path", category.getPath());

		List<JSONObject> list = searialChild(category);
		jsonObject.put("children", list);
		return jsonObject;
	}

	private List<JSONObject> searialChild(Category category) {
		List<JSONObject> children = null;
		List<Category> list = getByParentId(category.getId());
		if (list != null && list.size() > 0) {
			children = new ArrayList<JSONObject>();
		}
		for (Category object : list) {
			JSONObject jsonObject = searialTcatgory(object);
			children.add(jsonObject);
		}

		return children;
	}
}
